<template>
  <page-header-wrapper :key="key">
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
                <a-form-item label="平台">
                  <!-- platformId -->
                  <AWM-select
                    v-model="queryParam.platformId"
                    placeholder="请选择平台"
                    funStr="platformByNoAuth"
                  ></AWM-select>
                </a-form-item>
              </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="短信类型">
                <a-select v-model="queryParam.isInland">
                  <a-select-option value="">全部</a-select-option>
                  <a-select-option v-for="it in isInlandList" :key="it.id" :value="it.id">{{
                    it.name
                  }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="handleSearch">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (this.queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd">新建</a-button>
      </div>
      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns | columnsFilter"
        :data="loadData"
        showPagination="auto"
        :scroll="{ x: true }"
        :pagination="{ 'show-total': (total) => `Total ${total} items` }"
      >
        <span slot="action" slot-scope="text, record">
          <template>
            <span>
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical" />
            </span>
            <span>
              <a @click="handleDel(record)">删除</a>
            </span>
          </template>
        </span>
        <span slot="isInland" slot-scope="text">
          <a-tag :color="text | statusFilter(isInlandList, 'status')">
            {{ text | statusFilter(isInlandList, 'name') }}
          </a-tag>
        </span>
      </s-table>
      <product-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleCancel"
        @ok="handleOk"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import { userStatusList, isInlandList } from '@/utils/other'
import { getProductList, productAdd, productUpdate } from '@/api/sms'
import ProductForm from './modules/ProductForm'
import AWMSelect from '@/components/AWMSelect'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    key: 'id',
    width: 75,
  },
  {
    title: '名称',
    dataIndex: 'name',
    key: 'name',
    width: 150,
  },
  {
    title: '平台名称',
    dataIndex: 'platformName',
    width: 150,
  },
  {
    title: '短信类型',
    dataIndex: 'isInland',
    width: 150,
    scopedSlots: { customRender: 'isInland' },
  },

  {
    title: '平台单价(元)',
    dataIndex: 'platformPrice',
    width: 150,
    key: 'platformPrice',
  },
  {
    title: '用户初始单价(元)',
    dataIndex: 'userPrice',
    width: 150,
    key: 'userPrice',
  },
  {
    title: '备注',
    dataIndex: 'remark',
    width: 250,
    key: 'remark',
  },
   {
    title: '添加时间',
    dataIndex: 'addTime',
    width: 180,
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 240,
    scopedSlots: { customRender: 'action' },
  },
]

export default {
  name: 'AWM-userList',
  components: {
    STable,
    ProductForm,
    AWMSelect
  },
  data() {
    // this.columns = columns
    return {
      value: 1,
      projectTypeList: [],
      projectTypeList1: [],
      pathName: '',
      columns,
      timeout: null,
      inviteUserList: [],
      isInlandList,
      fetching: false,
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,

      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      parameter: {
        pageNum: 1,
        pageSize: 10,
      },
      queryParam: {},
      levelList: [], //  等级
      roleList: [], // 权限列表
      studioList: [], //  工作室列表
      // 状态
      userStatusList,
      isInlandList,
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        let sortJ = this.sortJ
        if (parameter.sortField) {
          parameter.sortJ = sortJ
        }
        const requestParameters = Object.assign(parameter, this.queryParam)
        return getProductList(requestParameters).then((res) => {
          let result = {
            data: res.data.data,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalCount: res.data.total,
            totalPage: res.data.pages,
          }
          return result
        })
      },
      key: 0,
    }
  },
  filters: {
    columnsFilter: function (a) {
      if (a) {
        return a.filter((v) => v.check || v.check == undefined)
      } else {
        return []
      }
      //this.columns.filter(v=>v.check || v.check == undefined)
    },
  },
  created() {},
  methods: {
    onChange() {},
    handleCeshi(record) {
      this.ceshi.visible = true
      this.ceshi.mdl = null
    },
    handleSearch() {
      this.$refs.table.refresh(true)
    },

    handleAdd() {
      this.mdl = null
      this.visible = true
    },
    handleEdit(record) {
      this.visible = true
      this.mdl = { ...record }
    },
    ModalCB(a_bool) {
      let self = this
      const form = self.$refs[`createModal`].form
      form.resetFields()
      self.visible = false
      self.confirmLoading = false
      a_bool && self.$refs.table.refresh()
    },
    handleOk() {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          if (values.id > 0) {
            productUpdate(values)
              .then((res) => {
                this.ModalCB(true)
                this.$message.success('修改成功!')
              })
              .catch((err) => {
                this.ModalCB()
              })
          } else {
            delete values.id
            productAdd(values)
              .then((res) => {
                this.ModalCB(true)
                this.$message.success('新增成功!')
              })
              .catch((err) => {
                this.ModalCB()
              })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date()),
      }
    },
    handleDel(a_record) {
      let self = this
      const modal = self.$confirm({
        title: '产品删除',
        content: `是否删除产品:${a_record.name}`,
        okText: '确认',
        cancelText: '取消',
        onOk: () => self.handleDelete(a_record.id, modal, -1),
        onCancel: () => {},
      })
    },
    handleDelete(a_id, a_modal, a_type) {
      return new Promise((resolve, reject) => {
        let data = { id: a_id, status: a_type }
        productUpdate(data)
          .then((res) => {
            this.$refs.table.refresh()
            this.$message.success('删除成功!')
            resolve()
          })
          .catch((err) => {
            a_modal.destroy()
            this.$message.error('操作失败!')
          })
      })
    },
  },
  watch: {
    '$route.name': function (n, o) {
      this.pathName = n
      this.key++
    },
  },
}
</script>
<style scoped>
a {
}
</style>

 